/**
* file name  : GetColumns.java
* authors    : Luciano Xumerle
* created    : lun 25 nov 2013 14:32:48 CEST
*
* Copyright (c) 2004-2013 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package it.ciano.jenoware;

import java.util.HashMap;
import it.ciano.util.CFile;


public class GetColumns {

    //
    // private vars used to store the field names
    //
    Pedigree ped;

    /**
     * Class Constructor.
     *
     * @param pedigree a populated Pedigree object.
     * @param bp array of marker positions expressed in basepairs.
     * @param genoOrder ordered list of markers.
     */
    public GetColumns ( Pedigree pedigree ) {
        ped = pedigree;
    }


    /**
     * Overriding toString method
     *
     * @return all informations that will be contained into the destination files.
     */
    public String toString() {
        Sbj[] sbjs=ped.getListSbj();
        String[] phnt = ped.getPhenotypesList();
        String[] geno = ped.getMarkersList();
        StringBuffer res = new StringBuffer();

        // PHENOTYPE
        for ( int i=0; i<sbjs.length; i++ ) {
            String id= sbjs[i].getID();
            if ( phnt != null ) {
                for ( int p=0; p<phnt.length; p++ ) {
                    res.append ( id ) ;
                    res.append ( "\t"  ) ;
                    res.append ( phnt[p] ) ;
                    res.append ( "\t" ) ;
                    res.append ( sbjs[i].getPhenotype ( phnt[p] ).getValue()  );
                    res.append ( "\t" );
                    res.append ( "\n" );
                }
            }

            // GENOTYPE
            StringBuffer gen = new StringBuffer();
            if ( geno != null ) {
                for ( int g=0; g<geno.length; g++ ) {
                    res.append ( id );
                    res.append ( "\t"  ) ;
                    res.append ( geno[g] );
                    res.append ( "\t"  ) ;
                    Genotype gg=sbjs[i].getGenotype ( geno[g] );
                    res.append ( gg.getAllele ( 1 ).getValue() );
                    res.append ( "\t" );
                    res.append ( gg.getAllele ( 2 ).getValue() );
                    res.append ( "\t" );
                    res.append ( "\n" );
                }
            }
        }
        return res.toString();
    }

    /**
     * Creates all the expected files.
     *
     * @return true if all operations are ok.
     */
    public boolean createsFile() {
        return CFile.saveString ( "columns.csv", toString() );
    }

} // end class

